package dao;

import hibernate.HibernateUtil;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;

import entity.Comanda;
import entity.Mesa;
import entity.Mozo;

public class ComandaDAO {
	
	private static ComandaDAO instancia = null;
	private static SessionFactory sf = null;

	
	public static ComandaDAO getInstancia(){
		if(instancia == null)
			instancia = new ComandaDAO();
		return instancia;
	}
	
	public ComandaDAO() {
		// TODO Auto-generated constructor stub
		sf = HibernateUtil.getSessionFactory();
	}
	
	public Comanda buscarComanda(int numero){
		Session s = null;
		Comanda c = null;
		try{
			s = sf.getCurrentSession();
			Transaction t = s.beginTransaction();
			
			Query q = s.createQuery("select C from Comanda C where C.numero =:numero"); 
			q.setParameter("numero", numero);
			c = (Comanda) q.uniqueResult();
			t.commit();
			
		}catch(Exception e){
			System.out.println(e);
			System.out.println("ErrorDAO: ComandaDAO.buscarComanda");
		}
		
		return c;
	}
	
	public void updateComanda(Comanda comanda){
		Session s=null;
		try {
			s=sf.getCurrentSession(); 
			Transaction t=s.beginTransaction();
			s.update(comanda);
			t.commit();
		} catch (Exception e){
			System.out.println(e);
			System.out.println("Error ComandaDAO. Update Comanda");
		}
	}
	
}
